using System;
using System.Collections.Generic;
using System.Text;
using iTunesLib;
using Rapier.Kodachi.Shared;
using System.Drawing;

namespace Rapier.Kodachi.iTunes.Actions
{
    public class RateTrack : iTunesActionBase
    {
        #region Constructor
        public RateTrack(Plugin plugin)
            : base(plugin, "Rapier.Kodachi.iTunes.RateTrack", "Rate Current Track", "Set the star rating for the current track.")
        {
            IndirectItemUse = IndirectItemUse.Required;
            DefaultIndirectTextMode = false; 
            GetImage = delegate() { return trackInfoImage; };
        }
        #endregion

        #region Action Members
        public override bool IsValidIndirect(Item indirectItem)
        {
            if (indirectItem.Properties.ContainsKey("iTunesRating"))
                return true;
            else
                return false;

            //OLD way for indirect text entry
            //if (indirectItem.Properties.ContainsKey("Text"))
            //    return Microsoft.VisualBasic.Information.IsNumeric(indirectItem.Properties["Text"].ToString());
            //else
            //    return false;
        }

        public override void Execute(Item[] directItems, Item[] indirectItems)
        {
            if (base.IsItunesRunning())
            {
                if (indirectItems.Length == 1 && itunes.CurrentTrack != null)
                {
                    switch (indirectItems[0].Name)
                    {
                        case "1 Star":
                            itunes.CurrentTrack.Rating = 20;
                            break;
                        case "2 Star":
                            itunes.CurrentTrack.Rating = 40;
                            break;
                        case "3 Star":
                            itunes.CurrentTrack.Rating = 60;
                            break;
                        case "4 Star":
                            itunes.CurrentTrack.Rating = 80;
                            break;
                        case "5 Star":
                            itunes.CurrentTrack.Rating = 100;
                            break;
                        case "Clear":
                            itunes.CurrentTrack.Rating = 0;
                            break;
                    }
                }

                //Old way for indirect text entry
                //if (indirectItems.Length > 0)
                //{
                //    try
                //    {
                //        int newRating=0;
                //        if (Int32.TryParse(indirectItems[0].Properties["Text"].ToString(), out newRating))
                //        {
                //            if (newRating < 0) newRating = 0;
                //            else if (newRating > 5) newRating = 100;
                //            else newRating = newRating * 20;

                //            if (itunes.CurrentTrack != null)
                //            {
                //                itunes.CurrentTrack.Rating = newRating;
                //            }
                //        }
                //    }
                //    catch
                //    {
                //    }
                //}
            }
        }
        #endregion
    }
}
